package org.lex.swing.widgets;

import java.awt.Dimension;
import java.awt.Window;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JOptionPane;

import org.lex.awt.Location;
import org.lex.swing.ActionBuilder;
import org.lex.swing.Option;
import org.lex.swing.OptionType;
import org.lex.swing.Swings;


public class OptionDialog {
	private static String DEFAULT_TITLE = "Select an Option";

	private String title = DEFAULT_TITLE;
	private Icon icon = null;
	private String message = "";
	private OptionType optionType = OptionType.getDefaultOptionTyle();
	private Action[] actions = null;

	public OptionDialog() {
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setIcon(Icon icon) {
		this.icon = icon;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public void setActions(Action... actions) {
		this.actions = actions;
	}

	// �������( ����setIcon(Icon) ) �����������Ͻǵ�ͼ���
	public void setMessageType(int type) {
		switch (type) {
		case JOptionPane.ERROR_MESSAGE:
			this.icon = Swings.errorIcon();
			break;
		case JOptionPane.INFORMATION_MESSAGE:
			this.icon = Swings.informationIcon();
			break;
		case JOptionPane.WARNING_MESSAGE:
			this.icon = Swings.warningIcon();
			break;
		case JOptionPane.QUESTION_MESSAGE:
			this.icon = Swings.questionIcon();
			break;
		case JOptionPane.PLAIN_MESSAGE:
			this.icon = null;
			break;
		default:
			this.icon = null;
			break;
		}
	}

	public void setOptionType(int type) {
		this.optionType = OptionType.parseJOptionPaneType(type);
	}

	public void setOptionType(OptionType type) {
		this.optionType = type;
	}

	public Option show(Window parent) {
		OptionDialogUI dialog = new OptionDialogUI(parent, title, icon, message, optionType, actions);
		dialog.pack();
		Dimension d = dialog.getSize();
		d.width += 20;
		d.height += 10 * (optionType.getOptionCount() - 1);
		dialog.setSize(d);
		if (null != parent)
			Location.moveToParentCenter(parent, dialog);
		else
			Location.moveToScreenCenter(dialog);
		dialog.setVisible(true);
		Option opt = dialog.getOption();
		dialog.dispose();
		dialog = null;
		return opt;
	}

	public static void main(String[] args) {
		Swings.setUseBoldMetalFont(false);
		// Swings.tryToUseSystemLookAndFeel();

		OptionDialog dialog = new OptionDialog();
		dialog.setMessage(getMessage());
		dialog.setMessageType(JOptionPane.QUESTION_MESSAGE);
		dialog.setOptionType(OptionType.YES_NO_CANCEL_OPTION);
		// build actions
		Action save = new ActionBuilder("Save").setTooltip("Save the changes");
		Action cancel = new ActionBuilder("Cancel").setTooltip("Go back to Notepad");
		dialog.setActions(save, null, cancel);
		Option opt = dialog.show(null);
		ln(opt.getDisplayName());
	}

	private static String getMessage() {
		return "<html>This is a multi-line message<br>in <b>HTML<b></html>";
	}

	static void ln(Object o) {
		System.out.println(o);
	}
}
